package com.future.hair.server.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.config.result.ResultCode;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

@Slf4j
@Component
@RestController
public class RequestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 请求头校验
        StringBuffer url = request.getRequestURL();

        String token = request.getHeader("token");
        String version = request.getHeader("version");
        String device = request.getHeader("device");

        if (token == null || token.trim().equals("")){
            getResponse(CommonResult.failed(ResultCode.UNAUTHORIZED),response);
            return false;
        }

        if (null==JwtHelper.verifyJwt(token)){
            getResponse(CommonResult.failed(ResultCode.VALIDATE_FAILED),response);
            return false;
        }

        if (url.toString().contains("manager/")){
            if (JwtHelper.getByOpenId(token)>=6666){

            }else{
                getResponse(CommonResult.failed(ResultCode.FORBIDDEN),response);
                return false;
            }
        }

        if (url.toString().contains("mall") || url.toString().contains("reserveCategory") || url.toString().contains("category")){
            if (JwtHelper.getByOpenId(token)>=6666){

            }else if (JwtHelper.getByOpenId(token)==4444 && (url.toString().contains("mall/upData") || url.toString().contains("mall/mallId") || url.toString().contains("mall/user/**"))){

            }else {
                getResponse(CommonResult.failed(ResultCode.FORBIDDEN),response);
                return false;
            }
        }

        if (device == null || device.trim().equals("")){

        }else if (device.equals("Android") || device.equals("iOS")){
            if (version == null || version.trim().equals("")){
                getResponse(CommonResult.failed(ResultCode.VERION_TO_UPDATA),response);
                return false;
            }else if (Integer.parseInt(version) < 10){
                getResponse(CommonResult.failed(ResultCode.VERION_TO_UPDATA),response);
                return false;
            }else if (Integer.parseInt(version) < 20){
                getResponse(CommonResult.failed(ResultCode.VERION_UPDATA),response);
                return false;
            }
        }


        return true;
    }

    /**
     * 构造响应消息体
     *
     * @param result
     * @param response
     * @throws IOException
     */
    private void getResponse(CommonResult result, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        PrintWriter out = null;
        out = response.getWriter();
        out.write(objectMapper.writeValueAsString(result));
        out.flush();
        out.close();
    }
}
